package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.model.TbItem;
import com.dongdongshop.model.TbItemCat;
import com.dongdongshop.model.TbItemCatExample;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.vo.CatVo;
import com.dongdongshop.vo.ItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {


    @Resource
    private TbItemCatMapper tbItemCatMapper;
    @Resource
    private TbItemMapper tbItemMapper;

    //扣减库存

    @Override
    public void subStockCount(List<ItemVo> itemVoList) {
        for (ItemVo temVo : itemVoList) {
            //根据Id查询数据
            TbItem item = tbItemMapper.selectByPrimaryKey(temVo.getId());
            //减库存
            item.setStockCount(item.getStockCount() - temVo.getNum());
            //修改数据库
            tbItemMapper.updateByPrimaryKeySelective(item);
        }
    }

    @Override
    public List<CatVo> query(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
//        TbItemCat tbItemCat=new TbItemCat();
//        BeanUtils.copyProperties(parentId, tbItemCat);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        return tbItemCats.stream().map(item ->{
            CatVo vo = new CatVo();
            BeanUtils.copyProperties(item,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void delcat(Long id) {
        tbItemCatMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<CatVo> CatList(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        return tbItemCats.stream().map(pp ->{
            CatVo vo = new CatVo();
            BeanUtils.copyProperties(pp,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addCat(CatVo catVo) {
        TbItemCat c = new TbItemCat();
        BeanUtils.copyProperties(catVo,c);
        tbItemCatMapper.insertSelective(c);
    }

    @Override
    public CatVo toupdate(Long id) {
            TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
            CatVo vo = new CatVo();
            BeanUtils.copyProperties(tbItemCat,vo);
            return vo;
        }

    @Override
    public void update(CatVo vo) {
        TbItemCat tbItemCat = new TbItemCat();
        BeanUtils.copyProperties(vo,tbItemCat);
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }
}

